package cn.cs.service.impl;

import cn.cs.client.*;
import cn.cs.common.*;
import cn.cs.pojo.Agent;
import cn.cs.service.AgentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AgentServiceImpl implements AgentService {

    @Resource
    private AgentClient agentClient;

    @Resource
    private SysDtAreaClient sysDtAreaClient;

    @Resource
    private AgentShopClient agentShopClient;

    @Resource
    private RestRepairUserClient repairUserClient;

    @Resource
    private RestShopClient shopClient;



    public Agent getAgentById(String id) throws Exception {
        return agentClient.getAgentById(id);
    }

    public List<Agent> getAgentListByMap(Map<String, Object> param) throws Exception {
        return agentClient.getAgentListByMap(param);
    }

    public Integer getAgentCountByMap(Map<String, Object> param) throws Exception {
        return agentClient.getAgentCountByMap(param);
    }

    public Integer addAgent(Agent agent) throws Exception {
        agent.setCreateDate(new Date());
        return agentClient.addAgent(agent);
    }

    public Integer modifyAgent(Agent agent) throws Exception {
        agent.setModifyDate(new Date());
        return agentClient.modifyAgent(agent);
    }

    public Integer deleteAgentById(String id) throws Exception {
        return agentClient.deleteAgentById(id);
    }

    public Long getMaxCode() throws Exception {
        return agentClient.getMaxCode();
    }

    public Page<Agent> queryAgentPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = agentClient.getAgentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Agent> agentList = agentClient.getAgentListByMap(param);
        page.setRows(agentList);
        return page;
    }

    public List<Map<String, Object>> getAgentAreaListById(String agentId) throws Exception {
        List<Map<String, Object>> list = agentClient.getAgentAreasByAgentId(agentId);
        for(Map<String, Object> a:list){
            String areaId = a.get("agent_area").toString();
            Map<String, Object> city = sysDtAreaClient.getCityNameByAreaId(areaId);
            a.put("provinceName", city.get("provinceName").toString()); a.put("cityName", city.get("cityName").toString());
            a.put("areaName", city.get("areaName").toString());
        }
        return list;
    }

    public Integer addAgentArea(Map<String, Object> param) throws Exception {
        return agentClient.addAgentArea(param);
    }

    public Integer deleteAgentAreaById(String id) throws Exception {
        return agentClient.deleteAgentAreaById(id);
    }

    public List<Map<String, Object>> getProvinceList() throws Exception {
        return sysDtAreaClient.getProvinceList();
    }
    public List<Map<String, Object>> getCityListByPid(String agentId) throws Exception {
        return sysDtAreaClient.getCityListByPid(agentId);
    }
    public List<Map<String, Object>> getAreaListByPid(String agentId) throws Exception {
        List<Map<String, Object>> list = agentClient.getAgentAreaAll();
        return sysDtAreaClient.getAreaListByPid(agentId, list);
    }

    public List<Map<String, Object>> getAreasListByPid(String agentId) throws Exception {
        return sysDtAreaClient.getAreasListByPid(agentId);
    }

    public Long getUserNumberByAgentCode(String invitationCode) throws Exception {
        return repairUserClient.getUserNumberByAgentCode(invitationCode);
    }

    public Long getShopNumberByAgentCode(String invitationCode) throws Exception {
        return shopClient.getShopNumberByAgentCode(invitationCode);
    }

    public Long getAgentBNumberByAgentCode(String invitationCode) throws Exception {
        return agentClient.getAgentBNumberByAgentCode(invitationCode);
    }

    public Long getAgentNumberByAgentCode(String invitationCode) throws Exception {
        List<Agent> list = agentClient.getAgentByPCode(invitationCode);
        return agentShopClient.getAgentNumberByAgentId(invitationCode, list);
    }

    public List<Map<String, Object>> getNextAreaList(String agentId, String pId, String type)throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> agentArea = agentClient.getAgentAreasByAgentId(agentId);
        switch (type){
            case "1":list = sysDtAreaClient.getAreaProvince(agentArea);break;
            case "2":list = sysDtAreaClient.getAreaCity(agentArea, pId);break;
            case "3":list = sysDtAreaClient.getAreaArea(agentArea, pId);break;
        }
        return list;
    }

    public Dto setAgentByAdmin(String id)throws Exception{
        Map<String, Object> map = agentClient.getAgentAreaById(id);
        String parent_invitation_code = map.get("parent_invitation_code").toString();
        String agent_area = map.get("agent_area").toString();
        Map<String, Object> map1 = agentClient.getAgentAreaByInvitationCode(parent_invitation_code, agent_area);
        String old_invitation_code = map1.get("invitation_code").toString();

        int j = agentClient.updateAgentType(parent_invitation_code, agent_area, old_invitation_code, id);

        return DtoUtil.returnSuccess("操作成功", "");
    }

    public String getAgentAreaTwoById(String id) {
        return agentClient.getAgentAreaTwoById(id);
    }

    public Map<String, Object> getAgentBYPhone(String phone) throws Exception{
        return agentClient.getAgentBYPhone(phone);
    }

    public String getAreaByProvinceId(String provinceId){
        return agentClient.getAreaByProvinceId(provinceId);
    }

    public Map<String, Object> getFirstByProvinceId(String id) throws Exception{
        return agentClient.getFirstByProvinceId(id);
    }

    public Integer insertAgentAreaList(List<Map<String, Object>> list){
        return agentClient.insertAgentAreaList(list);
    }

    public Page<Agent> queryAgentAllPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = agentClient.getAgentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Agent> agentList = agentClient.getAgentAllListByMap(param);
        page.setRows(agentList);
        return page;
    }

}
